package com.gsbelarus.gedemin.salary.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.gsbelarus.gedemin.lib.sync.protocol.SyncNotificationAdapter;
import com.gsbelarus.gedemin.salary.R;

import java.util.Map;

public class SyncNotification implements SyncNotificationAdapter {

    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    private Context context;

    public static final int FOREGROUND_NOTIFICATION_ID = 10001;
    public static final int ERROR_NOTIFICATION_ID = 10002;

    public static final int SEND_NOTIFICATION_ID = 10003;
    public static final int NEW_MSG_NOTIFICATION_ID = 10004;

    private String contentTitle;

    public SyncNotification(Context context) {
        this.context = context;
        mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(context);
        contentTitle = context.getString(R.string.app_name);
    }

    @Override
    public int getForegroundId() {
        return FOREGROUND_NOTIFICATION_ID;
    }

    @Override
    public Notification buildStartNotification(boolean isWorkMode) {
        clearForegroundNotification();
        String contentText;
        int iconResource;
        if (isWorkMode) {
            contentText = context.getString(R.string.notification_connect);
            iconResource = android.R.drawable.stat_sys_download;
        } else {
            contentText = context.getString(R.string.notification_create_demo);
            iconResource = android.R.drawable.ic_popup_sync;
        }
        mBuilder.setContentTitle(contentTitle)
                .setContentText(contentText)
                .setSmallIcon(iconResource)
                .setOngoing(true)
                .setProgress(0, 0, true)
                .setWhen(System.currentTimeMillis());
        return mBuilder.build();
    }

    @Override
    public void showUpdatingNotification() {
        mBuilder.setProgress(0, 0, true)
                .setContentText(context.getString(R.string.notification_update)); //TODO "Синхронизация"
        mNotifyManager.notify(FOREGROUND_NOTIFICATION_ID, mBuilder.build());
    }

    @Override
    public void showDownloadingNotification(int percent, int currentBlock, int totalBlocks) {
        mBuilder.setProgress(100, percent, false)
                .setContentText(context.getString(R.string.notification_download) + " " + currentBlock + " " +
                        context.getString(R.string.notification_from) + " " + totalBlocks);
        mNotifyManager.notify(FOREGROUND_NOTIFICATION_ID, mBuilder.build());
    }

    @Override
    public void showErrorNotification(String errorMessage) {
        mBuilder.setSmallIcon(R.mipmap.ic_sync_alert_white_24dp)
                .setProgress(0, 0, false)
                .setOngoing(false)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(contentTitle)
                .setContentText(errorMessage)
                .setCategory(NotificationCompat.CATEGORY_ERROR)
                .setLights(Color.RED, 500, 500)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .setBigContentTitle(contentTitle)
                        .bigText(errorMessage));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBuilder.setColor(context.getResources().getColor(R.color.dark_red));
        }
        mNotifyManager.notify(ERROR_NOTIFICATION_ID, mBuilder.build());
    }

    @Override
    public void clearForegroundNotification() {
        mNotifyManager.cancel(FOREGROUND_NOTIFICATION_ID);
    }


    public Notification getSendNotification(String contentText) { //TODO mBuilder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle(contentTitle)
                .setContentText(contentText)
                .setSmallIcon(android.R.drawable.stat_sys_upload)
                .setOngoing(true)
                .setProgress(0, 0, true)
                .setWhen(System.currentTimeMillis());
        return builder.build();
    }

    public void showNewMsgNotification(Map<Integer, String> notifyTexts, Class<?> activityClass, Class<?> fragmentClass) {
        NotificationManager mNotifyManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_message_text_white_24dp)
                .setCategory(NotificationCompat.CATEGORY_EMAIL)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setContentTitle(contentTitle)
                .setLights(Color.GREEN, 500, 500)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setColor(context.getResources().getColor(R.color.green));
        }

        Intent intent = new Intent(context, activityClass);
        intent.putExtra("openFragment", fragmentClass.getSimpleName());
        PendingIntent resultIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultIntent);
        builder.setAutoCancel(true);

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        if (notifyTexts.size() == 1) {
            inboxStyle.setBigContentTitle(context.getString(R.string.notification_new_msg));
            builder.setContentText(context.getString(R.string.notification_new_msg));
        } else {
            inboxStyle.setBigContentTitle(context.getString(R.string.notification_new_msgs, notifyTexts.size()));
            builder.setContentText(context.getString(R.string.notification_new_msgs, notifyTexts.size()));
        }

        for (Map.Entry<Integer, String> entry : notifyTexts.entrySet()) {
            inboxStyle.addLine(entry.getValue());
        }

        builder.setStyle(inboxStyle);
        mNotifyManager.notify(NEW_MSG_NOTIFICATION_ID, builder.build());
    }
}
